package com.zhubayi.shirodb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhubayi.shirodb.entity.Perms;
import com.zhubayi.shirodb.entity.User;
import com.zhubayi.shirodb.mapper.UserMapper;
import com.zhubayi.shirodb.service.UserService;
import com.zhubayi.shirodb.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhubayi
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     *
     * @param user
     */
    @Override
    public void register(User user) {
        //1.生产随机盐
        String salt = SaltUtils.getSalt(8);
        //2.把随机盐保存到数据库
        user.setSalt(salt);
        //3.明文密码进行md5 + salt + hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());
        userMapper.save(user);
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public User findRolesByUserName(String username) {
        return userMapper.findRolesByUserName(username);
    }

    @Override
    public List<Perms> findPermsByRoleId(String id) {
        return userMapper.findPermsByRoleId(id);
    }
}
